package leetcode;

import java.util.HashSet;

/**
 * @author niann
 * @description 删除排序链表中的重复元素
 * //TODO https://leetcode.cn/problems/remove-duplicates-from-sorted-list/description/
 * @since 2024/8/28 下午6:26
 **/
public class LeetCode83 {

    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    /**
     * 给定一个已排序的链表的头 head ， 删除所有重复的元素，使每个元素只出现一次 。返回 已排序的链表 。
     * @param head
     * @return
     */
    public ListNode deleteDuplicates(ListNode head) {
        if (head==null||head.next==null){
            return head;
        }
        var current = head;
        var next = current.next;
        while (next!=null){
            if (current.val==next.val){
                next=next.next;
                current.next=next;
            }else {
                current=current.next;
                next=next.next;
            }
        }
        return head;
    }
}
